package tbdp.tool.core.main;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;

import java.util.Date;

import tbdp.tool.util.DateFormatUtil;

/**
 * @author xbkaishui
 * @version $Id: CommandLineParser.java, v 0.1 2017-06-25 上午11:41 xbkaishui Exp $$
 */
public class CommandLineParser {


    public static RunConfig parse(String[] args) {

        RunConfig runConfig = new RunConfig();

        GnuParser parser = new GnuParser();
        Options opts = new Options();
        OptionBuilder.withLongOpt("date");
        OptionBuilder.withDescription("specify run date");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("ARG");
        opts.addOption(OptionBuilder.create("d"));

        OptionBuilder.withLongOpt("type");
        OptionBuilder.withDescription("specify table type id ");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("ARG");
        opts.addOption(OptionBuilder.create("t"));

        try {
            Date cur = new Date();
            String curDateStr = DateFormatUtil.format(cur, "yyyyMMdd");
            CommandLine cml = parser.parse(opts, args);
            runConfig.setDate(cml.getOptionValue("date", curDateStr));
            runConfig.setDataType(cml.getOptionValue("type"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return runConfig;
    }
}
